#!/bin/sh

#mkdir -p -- "$WORKDIR/ALTLinux/base"

comps="$(/.host/find "$WORKDIR/ALTLinux/" -mindepth 1 -maxdepth 1 -type d -name 'RPMS.*' -printf '%f ' |
	sed -e 's/RPMS\.//g')"

if [ -d "$WORKDIR/ALTLinux/RPMS.base" ]; then
	/.host/find "$WORKDIR/ALTLinux/RPMS.base" -type f -name '*.rpm' -printf '%f\n' |
	while read fn; do

		for d in $comps; do
			f="$WORKDIR/ALTLinux/RPMS.$d/$fn"

			[ "$d" = "base" ] || [ ! -f "$f" ] ||
				printf %s\\n "$f"
		done |
		xargs -r rm -vf --
	done
fi

if [ -z "$INFO_ARCH" ]; then
	INFO_ARCH="$(/.host/find "$WORKDIR"/ALTLinux/RPMS.* -type f -name '*.rpm' -printf '%f\n' |
			sed -ne 's/.*\.\([^.]\+\)\.rpm$/\1/p' |
			sort -u |
			tr '\n' ' ')"
	INFO_ARCH="${INFO_ARCH% }"
fi

genbasedir \
	--topdir="$WORKDIR" \
        --architecture="$INFO_ARCH" \
        --no-oldhashfile \
        --partial \
        --bz2only \
	--create \
        --notautomatic=false \
        ${INFO_NAME:+--archive="$INFO_NAME"} \
        ${INFO_VERSION:+--version="$INFO_VERSION"} \
        ${INFO_ORIGIN:+--origin="$INFO_ORIGIN"} \
        ${INFO_LABEL:+--label="$INFO_LABEL"} \
        ALTLinux $comps
